package com.mo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mo.entity.Category;
import com.mo.entity.User;
import com.mo.entity.Video;
import com.mo.mapper.VideoMapper;
import com.mo.feignservice.CategoryFeignService;
import com.mo.feignservice.UserFeignService;
import com.mo.service.IVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 视频 服务实现类
 * </p>
 *
 * @author gange
 * @since 2022-08-27
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements IVideoService {
    @Autowired
    private CategoryFeignService categoryFeignService;
    @Autowired
    private UserFeignService userFeignService;
    /**
     * 分页查询视频信息，视频的用户也关联起来，视频的类别信息也关联起来
     * @param page  分页信息
     * @param wrapper 查询条件
     * @return video的分页信息，在video的page对象里面Recodes属性里面放着全部的视频信息。
     */
    @Override
    public IPage<Video> pageVideoCateUser(Page<Video> page, QueryWrapper<Video> wrapper) {
        IPage<Video> videoIPage = baseMapper.selectPage(page, wrapper);
        List<Video> records = videoIPage.getRecords();
        if(ObjectUtils.isNotEmpty(records)){
            for(Video video:records){
                //查询每个视频的类别和用户信息。
                Integer categoryId = video.getCategoryId();
                Category category = categoryFeignService.selectOne(categoryId);
                video.setCategory(category.getName());
                User user = userFeignService.selectOne(video.getUid());
                video.setUploader(user);
            }
        }
        return videoIPage;
    }
}

